package csc.daonjpa.java.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import csc.daonjpa.java.dao.AccountDAO;
import csc.daonjpa.java.dao.CustomerDAO;
import csc.daonjpa.java.dao.InboxDAO;
import csc.daonjpa.java.dao.TransactionDAO;
import csc.daonjpa.java.domain.Account;
import csc.daonjpa.java.domain.Customer;
import csc.daonjpa.java.domain.Inbox;
import csc.daonjpa.java.domain.Transaction;

@Controller
@RequestMapping(value = "/transfer")
public class TransactionController {

	@Autowired
	private AccountDAO accountDAO;

	@Autowired
	private TransactionDAO transactionDAO;

	@Autowired
	private InboxDAO inboxDAO;

	@Autowired
	private CustomerDAO customerDAO;

	@RequestMapping
	public String goTransferPage(Model model) {
		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		String loginId = auth.getName();
		List<Account> accounts = accountDAO.getAccounts(loginId);
		model.addAttribute("accounts", accounts);
		return "transaction";
	}

	@RequestMapping(value = "/checktransfer", method = RequestMethod.POST)
	public String checkTranfer(HttpServletRequest request,
			final RedirectAttributes attr) {
		String accountNumberTarget = request
				.getParameter("accountNumberTarget");
		String loginIdTarget = request.getParameter("loginId");
		String bank = request.getParameter("bank");
		String branch = request.getParameter("branch");
		int amount = Integer.parseInt(request.getParameter("amount"));

		String accountNumberSource = request
				.getParameter("accountNumberSource");

		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		String loginIdSource = auth.getName();

		Account accountTarget = accountDAO.getAccount(accountNumberTarget,
				loginIdTarget);
		Account accountSource = accountDAO.getAccount(accountNumberSource,
				loginIdSource);
		if (accountTarget == null || accountSource == null || bank.equals("")
				|| branch.equals("")) {
			attr.addFlashAttribute(
					"message",
					"Sorry! Wrong account number or account ower. And please insert full information");
			return "redirect:/transfer";
		} else {
			int amountTarget = Integer.parseInt(accountTarget.getAmount());
			int amountSource = Integer.parseInt(accountSource.getAmount());

			accountTarget.setAmount((amountTarget + amount) + "");
			accountSource.setAmount((amountSource - amount) + "");

			accountDAO.updateAccount(accountTarget);
			accountDAO.updateAccount(accountSource);

			Transaction transaction = new Transaction(
					accountSource.getNumber(), accountTarget.getNumber(), bank,
					branch, amount + "", new Date(), loginIdSource,
					loginIdTarget);

			transactionDAO.addTransaction(transaction);
			Customer customer = customerDAO.getCustomer(loginIdSource);
			Inbox inbox = new Inbox("Your account " + accountTarget.getNumber()
					+ " recevied " + amount + " from account "
					+ accountSource.getNumber() + " of " + loginIdSource, "New",
					customer, loginIdTarget, new Date());
			inboxDAO.addInbox(inbox);
			attr.addFlashAttribute("message",
					"Congratulation! Transfer successfully");

			return "redirect:/transfer";
		}
	}
}
